<template>
  <div>
    <div style="margin: 10px 0;">
      <el-button type="primary" icon="el-icon-plus" @click="dialogVisible = true" disabled>新增</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="delBatch" style="margin-left: 5px" disabled>批量删除</el-button>
      <!--      <el-select v-model="name" placeholder="请选择学校场所" @change="select" style="margin-left: 10px">-->
      <!--        <el-option-->
      <!--            v-for="item in locales" :key="item.name" :label="item.name" :value="item.name">-->
      <!--        </el-option>-->
      <!--      </el-select>-->
      <el-input placeholder="请输入名称查询" v-model="localeName" clearable style="width: 200px;margin-left: 5px"></el-input>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 5px" @click="search">查询</el-button>
      <el-button type="warning" icon="el-icon-refresh" style="margin-left: 5px" @click="reset">重置</el-button>

    </div>
    <el-table :data="tableData" @selection-change="handleSelectionChange" style="width: 100%"
              :header-cell-style="{background:'#e7e8ef',color:'#4f5257'}"
              stripe fixed border>
      <el-table-column
          prop="id"
          type="selection"
          width="40">
      </el-table-column>
      <el-table-column prop="localeName" label="区域">
      </el-table-column>
      <el-table-column prop="description" label="描述">
      </el-table-column>
      <el-table-column prop="name" label="类别">
      </el-table-column>
      <el-table-column prop="state" label="是否属于风险区域" width="120">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.state" active-color="#13ce66" inactive-color="#ccc"
                     @change="changeEnable(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column
          label="操作"
          width="180">
        <template slot-scope="scope">
          <el-button type="success" class="el-icon-edit" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm
              style="margin-left: 5px"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="handleDelete(scope.row)"
          >
            <el-button type="danger" size="mini" class="el-icon-remove-outline" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>


    <el-dialog
        title="区域信息"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">

      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="场所选择">
          <template slot-scope="scope">
            <el-select clearable v-model="form.localeId" placeholder="请选择场所"  style="width: 100%;">
              <el-option v-for="i in locales"  :label="i.name" :value="i.id">
                <span style="float: left">{{ i.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ i.id }}</span>
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="场所区域名称">
          <el-input v-model="form.localeName"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
             <el-button type="primary" @click="save">确 定</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
import {serverIp} from "../../../public/config";

export default {
  name: "User",
  data(){
    return{
      serverIp: serverIp,
      name: "企业",
      tableData:[],
      localeName:'',
      pageNum:1,
      pageSize:10,
      total:0,
      dialogVisible: false,
      form: {},
      ids:[],
      multipleSelection: [],
      roles: [],
      locales:[]
    }
  },
  created() {
    this.load()
  },
  methods:{
    reset(){
      this.localeName="";
      this.load()
      this.$message({
        message: '重置成功',
        type: 'success'
      });
    },
    select(){
      this.load()
    },
    search(){
      if (this.name!=''||this.name!=null){
        this.load()
        this.$message({
          message: '查询成功',
          type: 'success'
        });
      }else{
        this.$message({
          message: '请输入查询内容',
          type: 'warning'
        });
      }
    },
    load(){
      this.request.get("/localName/page",{
            params:{
              pageNum:this.pageNum,
              pageSize:this.pageSize,
              localeName:this.localeName,
              name:this.name
            }
          }
      ).then(res =>{
        this.tableData=res.data.records
        this.total=res.data.total
      })
      //从后台获取场所类别
      this.request.get("/locale").then(res =>{
        this.locales = res.data
      })
    },
    save(){
      this.request.post("/localName",this.form).then(res => {
        if (res.code===200){
          this.$message.success("保存成功")
          this.dialogVisible=false
          this.form={}
          this.load()
        }else {
          this.$message.error(res.message)
        }
      })
    },
    handleEdit(row) {
      this.form=row
      this.dialogVisible=true
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    handleDelete(row) {
      console.log(row.id);
      this.request.delete("/localName/"+row.id).then(res=>{
        if (res.code===200){
          this.load()
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }
      })
    },
    delBatch(){
      let ids = this.multipleSelection.map(v =>v.id)
      if (ids.length!=0){
        this.$confirm('你正在进行批量删除操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.request.post("/localName/batch/", ids).then(res => {
            if (res.code===200){
              this.load()
              this.$message({
                type: 'success',
                message: '批量删除成功!'
              });
            }else {
              this.$message({
                type: 'error',
                message: res.message
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }else {
        this.$message.warning("请选择删除记录")
      }
    },
    /**
     * 选择框
     * */
    handleSelectionChange(val) {
      this.multipleSelection = val;
      for (let i=1; i<=val; i++)
        console.log(this.multipleSelection[i])
    },
    /**
     * 自定义操作列
     * @param index
     * @param row
     */
    handleSizeChange(val) {
      this.pageSize = val;
      this.load()
    },
    handleCurrentChange(val) {
      this.pageNum=val
      this.load()
    },
    handleAvatarSuccess(res){
      this.form.img=res
    },
    //上架
    changeEnable(row) {
      // this.request.post("/goods/status", row).then(res => {
      //   if (res.code === 200) {
      //     this.$message.success("操作成功")
      //   }
      // })
    },
  }
}
</script>

<style scoped>

</style>